package edu.gatech.volt2.drdoommgt.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import edu.gatech.volt2.drdoommgt.domain.Invoice;
import edu.gatech.volt2.drdoommgt.domain.Name;
import edu.gatech.volt2.drdoommgt.domain.Nurse;
import edu.gatech.volt2.drdoommgt.domain.Patient;
import edu.gatech.volt2.drdoommgt.domain.PatientInfo;
import edu.gatech.volt2.drdoommgt.domain.TreatmentRecord;


public class Users {
	@javax.persistence.PersistenceContext private static EntityManagerFactory emf = null;
	private static EntityManager em = null;
	
	@BeforeClass public static void connectToDatabase(){
		try{
			Users.emf = Persistence.createEntityManagerFactory("domain");
			Users.em = Users.emf.createEntityManager(); 	// Retrieve an application managed entity manager
		}catch(Throwable t){
			// Something didn't work.. these should work... fail
			t.printStackTrace();
			fail();
		}
	}
	
	
	@Test public void patientFindByName(){
		
		// Create a new nurse
		Patient testUser = new Patient(new Name("Nurse", "SomeoneTest"));
		Users.em.getTransaction().begin();
		Users.em.persist( testUser );
		Users.em.flush();
		Users.em.getTransaction().commit();
		
		// Create the name object to find the nurse by
		Name nameToFind = new Name("Nurse", "SomeoneTest");		
		List<Patient> foundList = Patient.findByName(nameToFind);

		assertEquals(1, foundList.size());
		assertEquals(testUser.getId(), foundList.get(0).getId());
		
		// Remove the testing user
		Users.em.getTransaction().begin();
		Users.em.remove(testUser);
		Users.em.getTransaction().commit();
		
		Users.em.close();

	}
	
	@Test public void createInvoice()
	{
		
		TreatmentRecord tr= new TreatmentRecord(null, null, null, null, "head hurts", "headache");
		List<TreatmentRecord> medHist= new ArrayList<TreatmentRecord>();
		medHist.add(tr);
		
		PatientInfo info= new PatientInfo("male", null, medHist, null, null, null);
		
		Patient p= new Patient(new Name("James","Bond"),info);
		
		Nurse n= new Nurse();
		
		Invoice i = n.createInvoice(p);
		
		Invoice testComparison= new Invoice(p, null, null, null);
		
		assertTrue(i.getPatient().toString().equals(testComparison.getPatient().toString()));

	}
}
